#include "loginwindow.h"
#include "ui_loginwindow.h"
#include <QMessageBox>

LoginWindow::LoginWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LoginWindow)
{
    ui->setupUi(this);
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);  // 密码输入隐藏
    connect(ui->lineEdit_2, &QLineEdit::returnPressed, this, &LoginWindow::on_pushButton_clicked);

}

LoginWindow::~LoginWindow()
{
    delete ui;
}

// void LoginWindow::on_pushButton_clicked()
// {
//     QString username = ui->lineEdit->text();
//     QString password = ui->lineEdit_2->text();

//     if (username == "admin" && password == "123456") {
//         emit loginSuccess();  // 通知主程序登录成功
//         this->close();        // 关闭登录窗口
//     } else {
//         QMessageBox::warning(this, "登录失败", "用户名或密码错误！");
//     }
// }

void LoginWindow::on_pushButton_clicked()
{
    QString username = ui->lineEdit->text();
    QString password = ui->lineEdit_2->text();

    static UserSql db;  // 保证只初始化一次数据库
    if (db.checkUserPassword(username, password)) {
        emit loginSuccess();
        this->close();
    } else {
        QMessageBox::warning(this, "登录失败", "用户名或密码错误！");
    }
}
